package com.example.demo.service.impl;

import com.example.demo.bean.Regional;
import com.example.demo.bean.SecurityDomain;
import com.example.demo.common.ServerResponse;
import com.example.demo.dao.SecurityDomainRepository;
import com.example.demo.service.SecurityDomainService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * Created by IntelliJ IDEA.
 *
 * @author ouyangzhixiang
 * @date 2018/6/23
 */
@Service
@AllArgsConstructor
public class SecurityDomainImpl implements SecurityDomainService {
    private final SecurityDomainRepository securityDomainRepository;
    @Override
    public ServerResponse<List<SecurityDomain>> list(Integer parentId) {
        List<SecurityDomain> securityDomainList = securityDomainRepository.findByParentIdOrderById(parentId);
        if (securityDomainList.isEmpty()){
            return ServerResponse.createByErrorMessage("没有相关记录");
        }
        return ServerResponse.createBySuccessMessage("成功",securityDomainList);
    }

    @Override
    public ServerResponse findOne(Integer id) {
        Optional<SecurityDomain> securityDomain = securityDomainRepository.findById(id);
        if (securityDomain.isPresent()){
            return ServerResponse.createBySuccessMessage("成功",securityDomain);
        }
        return ServerResponse.createByErrorMessage("没有相关记录");
    }

    @Override
    public ServerResponse<SecurityDomain> addSecurityDomain(SecurityDomain securityDomain) {
        SecurityDomain securityDomainAdd = securityDomainRepository.save(securityDomain);
        return ServerResponse.createBySuccessMessage("添加成功",securityDomainAdd);
    }

    @Override
    public void deleteSecurityDomain(Integer id) {
        securityDomainRepository.deleteById(id);
    }

    @Override
    public ServerResponse<SecurityDomain> SecurityDomainUpdate(SecurityDomain securityDomain) {
        SecurityDomain securityDomainUpdate = new SecurityDomain();
        securityDomainUpdate.setId(securityDomain.getId());
        securityDomainUpdate.setName(securityDomain.getName());
        securityDomainUpdate.setParentId(securityDomain.getParentId());
        securityDomain = securityDomainRepository.save(securityDomainUpdate);
        return ServerResponse.createBySuccessMessage("更新成功",securityDomain);
    }
}
